import 'package:flutter/material.dart';
import 'package:tiktok_clone/resources/strings.dart';
import 'package:video_player/video_player.dart';

class AppVideoPlayer extends StatefulWidget {
  @override
  _AppVideoPlayerState createState() => _AppVideoPlayerState();
}

class _AppVideoPlayerState extends State<AppVideoPlayer> {
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
//        'https://www.sample-videos.com/video123/mp4/240/big_buck_bunny_240p_5mb.mp4'
        AppStrings.url + "/hls/v4.m3u8",
    )
      ..initialize().then((_) {
        _controller.play();
        //是否循环播放
        _controller.setLooping(true);

        //音量大小
//        _controller.setVolume();
       setState(() {});
      });
  }
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        center(),
        gestureProcessing(),
      ],
    );
  }


  Center center() {
    return Center(
      child: _controller.value.initialized
          ? AspectRatio(
        aspectRatio: _controller.value.aspectRatio,
        child: VideoPlayer(_controller),
      )
          : Container(
        color: Colors.black,
      ),

    );
  }

  //点击事件
  Widget gestureProcessing() {
   return GestureDetector(
     onTap: tap,
   );
  }

  void tap() {
    _controller.value.isPlaying ? _controller.pause():_controller.play();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

